package com.yitong.xjd.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yitong.xjd.domain.CustCreditRecord;
import com.yitong.xjd.domain.RequestDataOrg;
import com.yitong.xjd.domain.RequestDataXjd;
import com.yitong.xjd.domain.RequestDataZm;
import com.yitong.xjd.domain.RequestDataZx;
import com.yitong.xjd.domain.ReturnData4Org;
import com.yitong.xjd.domain.ReturnData4Rh;
import com.yitong.xjd.domain.ReturnData4Xjd;
import com.yitong.xjd.domain.ReturnData4Zm;
import com.yitong.xjd.domain.ReturnData4Zx;
import com.yitong.xjd.services.XinJinDaiService;

@RestController
@RequestMapping("/XACRM")
public class XinJinDaiController{
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private XinJinDaiService xinJinDaiService;
	@Autowired
	private ZxbgParseUtils zxbgParseUtils;
	
	
	/**
	 * 薪金贷额度查询接口
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value="/queryXjded.do",method = RequestMethod.POST)
	public void queryXjded(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/json;charset=UTF-8");  
        ObjectMapper objectMapper = new ObjectMapper();
        String ids=null;
        //接收post请求数据
        StringBuffer sb=new StringBuffer("");
		try {
			logger.info("读取请求数据流。。。");
			BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));  
			String temp;  
			while ((temp = br.readLine()) != null) {  
			    sb.append(temp);  
			}  
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
			ids="读取报文数据失败！";
		}  
        //请求数据转化为对象
		String acceptjson = sb.toString(); 
		logger.info("解析请求数据。。。");
		RequestDataXjd o=null;
        if (!acceptjson.equals("")) {
        	try {
				o=objectMapper.readValue(acceptjson, RequestDataXjd.class);
			} catch (Exception e) {
				e.printStackTrace();
				ids="提取报文数据失败";
			}
        }else{
        	ids="报文数据为空";
        }
        //更新数据库
        logger.info("查询薪金贷数据。。。");
        ReturnData4Xjd bean=null;
        if(o!=null){
        	bean=xinJinDaiService.queryXjded(o);
		}else{
			bean = new ReturnData4Xjd("","0",ids,"0");
		}
        //返回数据
        PrintWriter out=null;
        try {
        	logger.info("返回薪金贷结果。。。");
			String s = objectMapper.writeValueAsString(bean);
			out = response.getWriter();  
			out.write(s); 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				out.close();
				out=null;
			}
		}
	}
	
	
	/**
	 * 征信报文接受接口
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value="/postDataToCrm.do",method = RequestMethod.POST)
	public void postZxbg(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/json;charset=UTF-8");  
        //接收post请求数据
        StringBuffer sb=new StringBuffer("");
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));  
			String temp;  
			while ((temp = br.readLine()) != null) {  
			    sb.append(temp);  
			}  
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("读取报文数据失败！");
		}  
        //请求数据转化为对象
		CustCreditRecord o=null;
        String acceptjson = sb.toString(); 
        if (!acceptjson.equals("")) {
        	try {
				o=zxbgParseUtils.getCustCreditRecord(acceptjson);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("提取报文数据失败");
			}
        }else{
        	System.out.println("报文数据为空");
        }
        //更新数据库
        ReturnData4Rh bean=null;
        if(o!=null){
        	bean=xinJinDaiService.dealRHData(request,o);
		}else{
			bean = new ReturnData4Rh("","0");
		}
        //返回数据
        PrintWriter out=null;
        try {
			String s = "<node><custidtno>"+bean.getCustIdtNo()+"</custidtno><status>"+bean.getStatus()+"</status></node>";
			out = response.getWriter();  
			out.write(s); 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				out.close();
				out=null;
			}
		}
	}

	
	/**
	 * 信贷机构查询接口
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value="/queryXdjg.do",method = RequestMethod.POST)
	public void queryXdjg(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/json;charset=UTF-8");  
        ObjectMapper objectMapper = new ObjectMapper();
        String ids=null;
        //接收post请求数据
        StringBuffer sb=new StringBuffer("");
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));  
			String temp;  
			while ((temp = br.readLine()) != null) {  
			    sb.append(temp);  
			}  
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
			ids="读取报文数据失败！";
		}  
        //请求数据转化为对象
		RequestDataOrg o=null;
        String acceptjson = sb.toString();
        if (!acceptjson.equals("")) {
        	try {
        		o=objectMapper.readValue(acceptjson, RequestDataOrg.class);
			} catch (Exception e) {
				e.printStackTrace();
				ids="提取报文数据失败";
			}
        }else{
        	ids="报文数据为空";
        }
        //更新数据库
        ReturnData4Org bean=null;
        if(o!=null){
        	bean=xinJinDaiService.queryXdjg(o);
		}else{
			bean = new ReturnData4Org("","0",ids,"0");
		}
        //返回数据
        PrintWriter out=null;
        try {
			String s = objectMapper.writeValueAsString(bean);
			out = response.getWriter();  
			out.write(s); 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				out.close();
				out=null;
			}
		}
	}
	
	
	/**
	 * 征信报告查询接口
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value="/queryRhzx.do",method = RequestMethod.POST)
	public void queryRhzx(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/json;charset=UTF-8");  
        ObjectMapper objectMapper = new ObjectMapper();
        String ids=null;
        //接收post请求数据
        StringBuffer sb=new StringBuffer("");
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));  
			String temp;  
			while ((temp = br.readLine()) != null) {  
			    sb.append(temp);  
			}  
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
			ids="读取报文数据失败！";
		}  
        //请求数据转化为对象
		RequestDataZx o=null;
        String acceptjson = sb.toString();
        if (!acceptjson.equals("")) {
        	try {
        		o=objectMapper.readValue(acceptjson, RequestDataZx.class);
			} catch (Exception e) {
				e.printStackTrace();
				ids="提取报文数据失败";
			}
        }else{
        	ids="报文数据为空";
        }
        //更新数据库
        ReturnData4Zx bean=null;
        if(o!=null){
        	bean=xinJinDaiService.queryRhzx(request,o);
		}else{
			bean = new ReturnData4Zx("","0",ids,"0");
		}
        //返回数据
        PrintWriter out=null;
        try {
			String s = objectMapper.writeValueAsString(bean);
			out = response.getWriter();  
			out.write(s); 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				out.close();
				out=null;
			}
		}
	}
	
	
	/**
	 * 芝麻积分查询接口
	 * @param request
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value="/queryZmxy.do",method = RequestMethod.POST)
	public void queryZmjf(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/json;charset=UTF-8");  
        ObjectMapper objectMapper = new ObjectMapper();
        String ids=null;
        
        //接收post请求数据
        StringBuffer sb=new StringBuffer("");
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8"));  
			String temp;  
			while ((temp = br.readLine()) != null) {  
			    sb.append(temp);  
			}  
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
			ids="读取报文数据失败！";
		}  
        //请求数据转化为对象
		RequestDataZm o=null;
        String acceptjson = sb.toString();
        System.out.println("");
        System.out.println("recv_str_is: "+acceptjson);
        if (!acceptjson.equals("")) {
        	try {
        		o=objectMapper.readValue(acceptjson, RequestDataZm.class);
			} catch (Exception e) {
				e.printStackTrace();
				ids="提取报文数据失败";
			}
        }else{
        	ids="报文数据为空";
        }
        //更新数据库
        ReturnData4Zm bean=null;
        if(o!=null){
        	bean=xinJinDaiService.queryZhimaScore(o);
		}else{
			bean = new ReturnData4Zm("-1", "", ids, "0");
		}
        //返回数据
        PrintWriter out=null;
        try {
        	System.out.println("crm_servlet_send: " + bean);
        	System.out.println("");
			String s = objectMapper.writeValueAsString(bean);
			out = response.getWriter();  
			out.write(s); 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				out.close();
				out=null;
			}
		}
	}
}
